<style>
table
{
	border-top:2px solid black;
	border-left:2px solid black;
	border-spacing:0px;
	text-align:center;
	margin:auto;
	font-weight:bold;
	width:100%;
}
th
{
	padding:10px;
	border-right:2px solid black;
	border-bottom:2px solid black;
}
td
{
	padding:10px;
	border-right:2px solid black;
	border-bottom:2px solid black;
}
</style>
<?php
include("../../app/connection.php");
class tbk
{
	public function _construct()
	{
	}
	public function getTKB($semeter,$year)
	{
		echo "<center><h1> Thời Khoá Biểu Học Kỳ ".$semeter." Năm Học ".$year."</h1></center>";
		$room = mysql_query("select distinct PHONG from LOP where HOCKY =".$semeter." and NAM ='".$year."' order by PHONG asc");
		echo "<table>";
		echo "<tr>";
		echo "<th>Phòng</th><th>Buổi</th><th>Ca</th><th>Thứ Hai</th><th>Thứ Ba</th><th>Thứ Tư</th><th>Thứ Năm</th><th>Thứ Sáu</th><th>Thứ Bảy</th>";
		echo "</tr>";
		while($row = mysql_fetch_array($room))
		{
			for($i=0;$i<4;$i++)
			{
				echo "<tr>";
				if($i==0)
				{
					echo "<th rowspan='4'>".$row[0]."</th>";
					echo "<th rowspan='2'>Sáng</th>";
					
				}
				if($i==2)
				{
					echo "<th rowspan='2'>Chiều</th>";
				}
				echo "<th>Ca ".($i+1)."</th>";
				for($j=0;$j<6;$j++)
				{
					$result = mysql_query("select * from LOP where CA like'%".($i+1)."%' and THU=".($j+2)." and PHONG =".$row[0]." and HOCKY =".$semeter." and NAM ='".$year."'");
					if(mysql_num_rows($result)==0)
					{
						echo "<td></td>";
					}
					else
					{

						while($col = mysql_fetch_assoc($result))
						{
							$subject = $col["MALOP"].".".$col["MAMONHOC"];
							$subjectName = mysql_query("select TENMONHOC from MONHOC where MAMONHOC='".$col["MAMONHOC"]."'");
							$subjectName = mysql_fetch_array($subjectName);
							$subjectName=$subjectName[0];
							$teacherName =mysql_query("select HOTEN from GIAOVIEN where MAGIAOVIEN='".$col["MAGIAOVIEN"]."'");
							$teacherName = mysql_fetch_array($teacherName);
							$teacherName=$teacherName[0];
							$cell = $subject."<br>".$subjectName."<br>GV.".$teacherName."<br>";
							$period = explode("-",$col["CA"]);
							if($period[0]==$i+1)
							{
								if(count($period)==1)
								{
									echo "<td>".$cell."</td>";
								}
								else
								{
									echo "<td rowspan='".count($period)."'>".$cell."</td>";
								}
							}
							
						}
					}
				}
				echo "</tr>";
			}
		}
		echo "</table>";
	}
	public function personalTKB($id,$semeter,$year)
	{
		echo "<center><h1> Thời Khoá Biểu Học Kỳ ".$semeter." Năm Học ".$year."</h1></center>";
		$room = mysql_query("select distinct PHONG from LOP,DANGKY where MSSV=$id and DANGKY.MALOP = LOP.MALOP and DANGKY.MAMONHOC = LOP.MAMONHOC and LOP.NAM=DANGKY.NAM and LOP.HOCKY=DANGKY.HOCKY and LOP.HOCKY =".$semeter." and LOP.NAM ='".$year."' order by PHONG asc");
		echo "<table>";
		echo "<tr>";
		echo "<th>Phòng</th><th>Buổi</th><th>Ca</th><th>Thứ Hai</th><th>Thứ Ba</th><th>Thứ Tư</th><th>Thứ Năm</th><th>Thứ Sáu</th><th>Thứ Bảy</th>";
		echo "</tr>";
		while($row = mysql_fetch_array($room))
		{
			for($i=0;$i<4;$i++)
			{
				echo "<tr>";
				if($i==0)
				{
					echo "<th rowspan='4'>".$row[0]."</th>";
					echo "<th rowspan='2'>Sáng</th>";
					
				}
				if($i==2)
				{
					echo "<th rowspan='2'>Chiều</th>";
				}
				echo "<th>Ca ".($i+1)."</th>";
				for($j=0;$j<6;$j++)
				{
					$result = mysql_query("select LOP.MALOP,LOP.MAMONHOC,LOP.MAGIAOVIEN,CA from LOP,DANGKY where LOP.MAMONHOC=DANGKY.MAMONHOC and LOP.MALOP = DANGKY.MALOP and LOP.NAM=DANGKY.NAM and LOP.HOCKY=DANGKY.HOCKY and MSSV=$id and CA like'%".($i+1)."%' and THU=".($j+2)." and PHONG =".$row[0]." and LOP.HOCKY =".$semeter." and LOP.NAM ='".$year."'");
					if(mysql_num_rows($result)==0)
					{
						echo "<td></td>";
					}
					else
					{

						while($col = mysql_fetch_assoc($result))
						{
							$subject = $col["MALOP"].".".$col["MAMONHOC"];
							$subjectName = mysql_query("select TENMONHOC from MONHOC where MAMONHOC='".$col["MAMONHOC"]."'");
							$subjectName = mysql_fetch_array($subjectName);
							$subjectName=$subjectName[0];
							$teacherName =mysql_query("select HOTEN from GIAOVIEN where MAGIAOVIEN='".$col["MAGIAOVIEN"]."'");
							$teacherName = mysql_fetch_array($teacherName);
							$teacherName=$teacherName[0];
							$cell = $subject."<br>".$subjectName."<br>GV.".$teacherName."<br>";
							$period = explode("-",$col["CA"]);
							if($period[0]==$i+1)
							{
								if(count($period)==1)
								{
									echo "<td>".$cell."</td>";
								}
								else
								{
									echo "<td rowspan='".count($period)."'>".$cell."</td>";
								}
							}
							
						}
					}
				}
				echo "</tr>";
			}
		}
		echo "</table>";
	}
}
?>